package com.buobao.screenlogcat

import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.google.gson.Gson
import com.turman.blibrary.log.BConsolePrinter
import com.turman.blibrary.log.BFilePrinter
import com.turman.blibrary.log.BLog
import com.turman.blibrary.log.BLogConfig
import com.turman.blibrary.log.BLogConfig.JsonParser
import com.turman.blibrary.log.BLogManager
import com.turman.blibrary.log.BViewPrinter
import com.turman.blibrary.util.BDisplayUtil
import com.turman.builibrary.tab.bottom.BTabBottomInfo
import com.turman.builibrary.tab.bottom.BTabBottomLayout

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化日志 放到application中去初始化
        BLogManager.init(object : BLogConfig() {
            override fun getGlobalTag(): String {
                return "buobao"
            }

            override fun enable(): Boolean {
                return true
            }

            override fun showThread(): Boolean {
                return true
            }

            override fun staceTraceDepth(): Int {
                return 6
            }

            override fun injectJsonParser(): JsonParser {
                return JsonParser { src -> Gson().toJson(src) }
            }
        }, BConsolePrinter(), BFilePrinter.getInstance(application))

//        setContent {
//            ScreenLogcatTheme {
//                // A surface container using the 'background' color from the theme
//                Surface(
//                    modifier = Modifier.fillMaxSize(),
//                    color = MaterialTheme.colorScheme.background
//                ) {
//                    Greeting()
//                }
//            }
//        }

        setContentView(R.layout.act_main)


        initTabBottom()


        //添加界面日志打印器 需要在页面关闭时移除
        BLogManager.getInstance().addPrinters(BViewPrinter(this))
    }

    private fun initTabBottom() {
        val bTabBottomLayout = findViewById<BTabBottomLayout>(R.id.btablayout)
        bTabBottomLayout.setBottomAlpha(0.85f)
        val bottomInfoList: MutableList<BTabBottomInfo<*>> = ArrayList()
        val homeInfo = BTabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            getString(R.string.if_home),
            null,
            "#ff656667",
            "#ffd44949"
        )

        val recommendIndo = BTabBottomInfo(
            "收藏",
            "fonts/iconfont.ttf",
            getString(R.string.if_recommend),
            null,
            "#ff656667",
            "#ffd44949"
        )

        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.fire,null)
        val categoryInfo = BTabBottomInfo<String>(
            "分类",
            bitmap,
            bitmap
        )

        val chatInfo = BTabBottomInfo(
            "消息",
            "fonts/iconfont.ttf",
            getString(R.string.if_chat),
            null,
            "#ff656667",
            "#ffd44949"
        )

        val profileInfo = BTabBottomInfo(
            "我的",
            "fonts/iconfont.ttf",
            getString(R.string.if_profile),
            null,
            "#ff656667",
            "#ffd44949"
        )

        bottomInfoList.add(homeInfo)
        bottomInfoList.add(recommendIndo)
        bottomInfoList.add(categoryInfo);
        bottomInfoList.add(chatInfo)
        bottomInfoList.add(profileInfo)

        bTabBottomLayout.inflateInfo(bottomInfoList)
        bTabBottomLayout.addTabSelected { index, preInfo, nextInfo ->
            Toast.makeText(
                this@MainActivity,
                "click ${nextInfo.name}",
                Toast.LENGTH_SHORT
            ).show()
        }
        bTabBottomLayout.defaultSelected(homeInfo)

        val tabBottom = bTabBottomLayout.findTab(bottomInfoList[2])
        tabBottom?.apply {
            resetHeight(BDisplayUtil.dp2px(60f,resources))
        }

    }

}

@Composable
fun Greeting(modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState()),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(onClick = {
//            BLog.i("init")
//            BLog.e(false)
//            BLog.w(1)
//            BLog.a(1.0f)
//            val map = mapOf("1" to "v1", "2" to "v2")
//            BLog.i(map)

            Thread {
                BLog.i("init")
                BLog.e(false)
                BLog.w(1)
                BLog.a(1.0f)
            }.start()

            Thread {
                BLog.i("init")
                BLog.e(false)
                BLog.w(1)
                BLog.a(1.0f)
            }.start()

            Thread {
                BLog.i("init")
                BLog.e(false)
                BLog.w(1)
                BLog.a(1.0f)
            }.start()

        }) {
            Text(text = "print log")
        }
    }
}
